<?php
namespace home;
use controllers\BaseControllers;
use models\BaseDao;


class Home extends BaseControllers{

// 覆盖父类的构造方法
public function __construct(){
$loader = new \Twig\Loader\FilesystemLoader(TEMPDIR.'/app/views/'.TEMPNAME);
$this->twig = new \Twig\Environment($loader, [
    // 'cache' => '/path/to/compilation_cache',
]);
$this->init();

}
protected function display($template){

$url = getCurUrl();
$this->assign('url',$url.'/app/views/'.TEMPNAME.'/resource');//自己模板下的css js images
$this->assign('public',$url.'/app/views/public');//所有模板公共的前端css js images
$this->assign('res',$url.'/uploads');//文件上传资源

echo $this->twig->render($template.'.html',$this->data);
}

function init(){
$db = new BaseDao();
// $_SESSION['user_token'] = md5($user['id'].$_SESSION['HTTP_HOST']);

// echo $_SERVER['HTTP_HOST']."<hr>";
$this->assign('login',ew_login('user'));
$this->assign('session',$_SESSION);
// 获取网站的设置信息setting
$allsetting = $db->select('setting','*');

$setting = array();

foreach($allsetting as $v ){
    $setting[$v['skey']] = $v['svalue'];
}

$setting['web_qqs'] = explode(',',$setting['web_qq']);

$this->assign('setting',$setting);
// 获取商品分类信息,制作菜单
$cats = $db->select('category',['id','catname'],['pid'=>0,'ORDER'=>['ord'=>'ASC','id'=>'DESC'],'LIMIT'=>10]);
$this->assign('cats',$cats);
// 所有广告
$this->assign('ads',$db->select('ad','*',['ORDER'=>['ord'=>'ASC','id'=>'ASC']]));
// 所有的单页信息
$this->assign('page',$db->select('page',['id','name'],['ORDER'=>['ord'=>'ASC']]));

// 友情链接
$this->assign('links',$db->select('link','*',['ORDER'=>['ord'=>'ASC']]));

// 用户登录的处理


// 获取购物车信息 
if(ew_login('user')){
    $cart_num = $db->count('cart',['uid'=>$_SESSION['id']]);
}else{
    // 从cookie中获取购物记录
    $cart_list = unserialize(stripcslashes($_COOKIE['cart_list']));
    if($cart_list){
        $cart_num = count($cart_list );
    }else{
        $cart_num = 0;
    }
}
$this->assign('cart_num',$cart_num);

// 记录访客的一次访问
$db->insert('iplog',['ip'=>get_real_ip(),'atime'=>time()]);

// 在所有的分类下搜索
$this->assign('pid',0);

}
}